//
//  LGHorizontalMenuView.m
//  UniverseProject
//
//  Created by king on 2019/9/4.
//  Copyright © 2019年 Cloud2209. All rights reserved.
//



@interface LGHorizontalMenuModel : NSObject

@property (copy,nonatomic) NSString *title;
@property (nonatomic, assign)CGFloat lab_width;

@end

@implementation LGHorizontalMenuModel


@end



@interface LGHorizontalMenuViewCell : UICollectionViewCell

@property (nonatomic, weak)UILabel *itemLab;

@end

@implementation LGHorizontalMenuViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *itemLab = [UILabel labelWithTitle:@"" color:LGCOLOR(@"ffffff") font:LGFONT(14)];
        itemLab.backgroundColor = LGCOLOR(@"#FF9F08");
        itemLab.textAlignment = 1;
        itemLab.cornerRadius = 3;
        [self.contentView addSubview:itemLab];
        self.itemLab = itemLab;
        
    }
    return self;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    self.itemLab.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
}

@end




#import "LGHorizontalMenuView.h"


@interface LGHorizontalMenuView()<UICollectionViewDelegate, UICollectionViewDataSource,UIScrollViewDelegate ,UICollectionViewDelegateFlowLayout>


@property (nonatomic, strong)UICollectionView *collectionView;
//选中的
@property (nonatomic, assign)NSIndexPath *hisSelIndexPath;

@end


@implementation LGHorizontalMenuView


// 注册cell
static NSString * const ID = @"LGHorizontalMenuView";


+ (instancetype)initWithOrginFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray{
    LGHorizontalMenuView *menu = [[LGHorizontalMenuView alloc] initWithFrame:frame];
    menu.dataArr = dataArray;
    menu.clipsToBounds = YES;
    [menu setupCollectionView];
    return menu;
}


- (void)setupCollectionView{
    
    UICollectionViewFlowLayout *layout = ({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//        layout.itemSize = CGSizeMake((K_SCREEN_WIDTH - 40) / 2, 229);
        layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
        layout.minimumLineSpacing = 0.02;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout;
    });
    
    CGRect collectionViewFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    UICollectionView *collectionView = ({
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:layout];
        self.collectionView = collectionView;
        collectionView.showsVerticalScrollIndicator = NO;
        [self addSubview:collectionView];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.alwaysBounceVertical = NO;
        collectionView;
    });
    // 注册cell
    [collectionView registerClass:[LGHorizontalMenuViewCell class] forCellWithReuseIdentifier:ID];
    
    
    
}



#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title = self.dataArr[indexPath.row];
    LGHorizontalMenuViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.itemLab.text = title;
    
    if (indexPath.row == self.hisSelIndexPath.row) {
        cell.itemLab.textColor = LGCOLOR(@"ffffff");
        cell.itemLab.backgroundColor = LGCOLOR(@"#FF9F08");
        NSLog(@"当前选中%ld",indexPath.row);
    }else{
        cell.itemLab.textColor = LGCOLOR(@"#666666");
        cell.itemLab.backgroundColor = LGCOLOR(@"#F0F0F0");
    }
    
    return cell;
}





- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *title = self.dataArr[indexPath.row];
    CGFloat titleW = [title sizeWithFont:LGFONT(14)].width;
    return CGSizeMake(titleW + 10, 30);
    
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.hisSelIndexPath = indexPath;
    [self.collectionView reloadData];
}






@end
